package com.taomu.app.zongchouhuli.moudle.mine.activity;

import android.app.Dialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.taomu.app.zongchouhuli.R;
import com.taomu.app.zongchouhuli.entity.BaseChangeResponse;
import com.taomu.app.zongchouhuli.framework.BaseActivity;
import com.taomu.app.zongchouhuli.http.res.ResMonthDistributeFeeEntity;
import com.taomu.app.zongchouhuli.http.res.ResSpreadEntity;
import com.taomu.app.zongchouhuli.http.res.ResUserInfoEntity;
import com.taomu.app.zongchouhuli.moudle.mine.adpter.SpreadAdp;
import com.taomu.app.zongchouhuli.moudle.mine.contract.UserSpreadContract;
import com.taomu.app.zongchouhuli.moudle.mine.contract.impl.UserSpreadPresenter;
import com.taomu.app.zongchouhuli.moudle.mine.entity.SpreadEntity;
import com.taomu.app.zongchouhuli.utils.ListViewUtils;
import com.taomu.app.zongchouhuli.utils.ParseUtil;
import com.taomu.app.zongchouhuli.utils.StorageHelper;
import com.taomu.app.zongchouhuli.weight.EmptyRecyclerView;

import java.util.List;

/**
 * 我的推广
 */
public class UserSpreadActivity extends BaseActivity<UserSpreadContract.Presenter> implements UserSpreadContract.View {

    private ImageView mBackIm;

    private TextView mPriceIm;

    private EmptyRecyclerView mRecy;

    private SmartRefreshLayout mRefreshLayout;

    private SpreadAdp mAdp;

    private TextView mZhuanruTv;

    private int ship = 0;

    private int take = 10;

    private String currentTotalMoney = "0";

    private TextView mCurrentMoneyTv;

    @Override
    protected void onDestroy() {
        mPresenter.onDetch();
        super.onDestroy();
    }

    @Override
    protected int getContentLayout() {
        return R.layout.activity_spread;
    }

    @Override
    protected void initView() {
        mRefreshLayout = findViewById(R.id.spread_refresh);
        mCurrentMoneyTv = findViewById(R.id.spread_list_current_income_tv);
        mBackIm = findViewById(R.id.spread_back_im);
        mPriceIm = findViewById(R.id.spread_income_tv);
        mZhuanruTv = findViewById(R.id.spread_zhuanru_tv);
        mAdp = new SpreadAdp(this);
        mRecy = findViewById(R.id.spread_recy);
        mRecy.setLayoutManager(new LinearLayoutManager(this));
        mRecy.setAdapter(mAdp);
        ListViewUtils.setDefaultEmpty(mRecy);
    }

    @Override
    protected void initData() {
        currentTotalMoney = ParseUtil.parseObject(StorageHelper.getAccountInfo(), ResUserInfoEntity.class).getDistribute_account();
        if(TextUtils.isEmpty(currentTotalMoney) || "null".equals(currentTotalMoney)){
            totalMoney = 0;
        }else{
            totalMoney = Double.valueOf(currentTotalMoney);
        }
        mCurrentMoneyTv.setText("¥"+currentTotalMoney);
        mPresenter.getMonthSpreadMoney();
        mRefreshLayout.autoRefresh();
    }

    @Override
    protected void initEvent() {
        mBackIm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mRefreshLayout.setOnRefreshListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {

            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                mRefreshLayout.setEnableLoadMore(true);
                ship = 0;
                mPresenter.getMonthSpreadMoney();
                mPresenter.getSpreadListData(ship,take);
            }
        });
        mRefreshLayout.setOnLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                mPresenter.getSpreadListData(ship,take);
            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {

            }
        });
        mZhuanruTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog();
            }
        });
    }

    @Override
    protected UserSpreadContract.Presenter createPresenter() {
        return new UserSpreadPresenter(this);
    }

    @Override
    public void setSpreadListData(ResSpreadEntity mData) {
        if(mData.getData() != null) {
            //mAdp.addData(mData);
            if (mRefreshLayout.isLoading()) {
                mAdp.addData(mData.getData());
            } else if (mRefreshLayout.isRefreshing()) {
                mAdp.setData(mData.getData());
            } else {
                mAdp.setData(mData.getData());
            }
            ship = mAdp.getItemCount();
            if(Integer.valueOf(mData.getCount())>mAdp.getItemCount()){
                mRefreshLayout.setEnableLoadMore(true);
            }else{
                mRefreshLayout.setEnableLoadMore(false);
            }
        }else{
            mRefreshLayout.setEnableLoadMore(false);
        }
    }

    private double totalMoney;

    @Override
    public void setMonthTotal(ResMonthDistributeFeeEntity msg) {
        if(TextUtils.isEmpty(msg.getData()) || "null".equals(msg.getData())){
            mPriceIm.setText("¥ 0");
        }else {
            mPriceIm.setText("¥ " + msg.getData());
        }
    }

    @Override
    public void setMsg(String msg) {
        mPresenter.getUserInfo();
        showToast(msg);
    }

    @Override
    public void setTotalMoney(String money) {
        if(TextUtils.isEmpty(money) || "null".equals(money)){
            totalMoney = 0;
        }else{
            totalMoney = Double.valueOf(currentTotalMoney);
        }
        mCurrentMoneyTv.setText("¥"+money);
    }

    @Override
    public void dismissLoadingDialog() {
        //super.dismissLoadingDialog();
        mRefreshLayout.finishLoadMore();
        mRefreshLayout.finishRefresh();
    }

    @Override
    public void showLoadingDialog() {
        //super.showLoadingDialog();
    }

    @Override
    public void setPresenter(UserSpreadContract.Presenter presenter) {

    }

    private Dialog dialog;

    private void showDialog(){
        if(dialog == null){
            dialog = new Dialog(this);
            View view = LayoutInflater.from(this).inflate(R.layout.dialog_zhuanru_layout, null);
            final EditText mMonyEt = view.findViewById(R.id.zhuanru_money_et);
            final TextView cancleTv = view.findViewById(R.id.zhuanru_cancle_tv);
            final TextView confirmTv = view.findViewById(R.id.zhuanru_confirm_tv);
            final ImageView mClose = view.findViewById(R.id.btn_close);
            mClose.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mMonyEt.setText("");
                    if(dialog != null){
                        dialog.dismiss();
                    }
                }
            });
            confirmTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String moneystr = mMonyEt.getText().toString();
                    if(!TextUtils.isEmpty(moneystr)){
                        int money = Integer.valueOf(moneystr);
                        if(money == 0){
                            showToast("金额不能为0");
                            return;
                        }
                        if(totalMoney>=money){
                            mMonyEt.setText("");
                            mPresenter.tixian(moneystr);
                            if(dialog != null){
                                dialog.dismiss();
                            }
                        }else{
                            showToast("输入金额不能大于产品总收益");
                        }

                    }else{
                        showToast("请输入金额");
                    }

                }
            });
            cancleTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mMonyEt.setText("");
                    if(dialog != null){
                        dialog.dismiss();
                    }
                }
            });
            dialog.setContentView(view);
            dialog.show();
        }else{
            dialog.show();
        }
    }
}
